package com.mxx.bu_yang.adapter;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.mxx.bu_yang.R;
import com.mxx.bu_yang.response.CheckAlreadyResponse;
import com.mxx.bu_yang.response.StockInfoResponse;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: Bu_Yang
 * @Package: com.mxx.bu_yang.adapter
 * @ClassName: CheckAlreadyParentAdapter
 * @Description: 已盘点
 * @Author: 刘江飞
 * @Version: 1.0
 */
public class CheckAlreadyParentAdapter extends BaseQuickAdapter<CheckAlreadyResponse, BaseViewHolder> {
    private final Activity activity;

    public CheckAlreadyParentAdapter(Activity activity, int layoutResId) {
        super(layoutResId);
        this.activity = activity;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, CheckAlreadyResponse response) {
        if (response == null) {
            return;
        }
        TextView tv_time = baseViewHolder.getView(R.id.tv_time);
        TextView divider_line1 = baseViewHolder.getView(R.id.divider_line1);
        TextView divider_line3 = baseViewHolder.getView(R.id.divider_line3);
//        ImageView iv_calendar = baseViewHolder.getView(R.id.iv_calendar);
        RecyclerView rv_check_child = baseViewHolder.getView(R.id.rv_check_child);
        TextView tv_stock_hint = baseViewHolder.getView(R.id.tv_stock_hint);

        if (baseViewHolder.getAdapterPosition() == 0) {
            divider_line1.setVisibility(View.INVISIBLE);
        } else {
            divider_line1.setVisibility(View.VISIBLE);
        }

        String createTime = response.createTime;
        if (createTime != null) {
            tv_time.setText(createTime);
        }
        String result = response.result;
        if (result != null) {
            tv_stock_hint.setText(result);
        }
        List<StockInfoResponse> list = getList(response);

        initStockInfo(rv_check_child, list);
        if (baseViewHolder.getAdapterPosition() == getItemCount() - 1) {
            divider_line3.setVisibility(View.INVISIBLE);
        } else {
            divider_line1.setVisibility(View.VISIBLE);
        }

    }

    private List<StockInfoResponse> getList(CheckAlreadyResponse response) {
        List<StockInfoResponse> list = new ArrayList<>();
        StockInfoResponse stockInfoResponse;
        String billNo = response.billNo;
        stockInfoResponse = new StockInfoResponse();
        stockInfoResponse.key = "盘点单号：";
        stockInfoResponse.value = billNo;
        list.add(stockInfoResponse);

        String storName = response.storName;
        stockInfoResponse = new StockInfoResponse();
        stockInfoResponse.key = "库位名称：";
        stockInfoResponse.value = storName;
        list.add(stockInfoResponse);

        String wareNumber = response.wareNumber;
        stockInfoResponse = new StockInfoResponse();
        stockInfoResponse.key = "批号：";
        stockInfoResponse.value = wareNumber;
        list.add(stockInfoResponse);

        String lotNo = response.lotNo;
        stockInfoResponse = new StockInfoResponse();
        stockInfoResponse.key = "盘点单号：";
        stockInfoResponse.value = lotNo;
        list.add(stockInfoResponse);

        int actualQty = response.actualQty;
        stockInfoResponse = new StockInfoResponse();
        stockInfoResponse.key = "实际盘点数：";
        stockInfoResponse.value = actualQty + "";
        list.add(stockInfoResponse);

        String difference = response.difference;
        stockInfoResponse = new StockInfoResponse();
        stockInfoResponse.key = "差异数：";
        stockInfoResponse.value = difference + "";
        list.add(stockInfoResponse);
        return list;
    }

    private void initStockInfo(RecyclerView rv_check_child, List<StockInfoResponse> list) {

        if (rv_check_child.getItemDecorationCount() == 0) {
            RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    int mode = parent.getChildAdapterPosition(view);
                    if (mode == 0) {
                        outRect.top = 13;
                        outRect.bottom = 0;
                    } else {
                        outRect.top = 15;
                        if (mode == state.getItemCount() - 1) {
                            outRect.bottom = 15;
                        } else {
                            outRect.bottom = 0;
                        }
                    }
                }
            };
            rv_check_child.addItemDecoration(decoration);
        }

        LinearLayoutManager mManager = new LinearLayoutManager(activity);
        mManager.setOrientation(RecyclerView.VERTICAL);
        rv_check_child.setLayoutManager(mManager);
        StockInfoAdapter infoAdapter = new StockInfoAdapter(R.layout.adapter_stock_info);
        rv_check_child.setAdapter(infoAdapter);
        infoAdapter.setList(list);
    }
}
